//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;

namespace Microsoft.Research.DataLayer
{
    public partial class AssemblyPackage
    {
        # region CommonSearches
        public sealed class CommonSearches
        {

            private CommonSearches()
            {
            }

            public static Collection<AssemblyPackage> GetAssemblyPackages(Connection registryConnection)
            {
                #region Validation of Arguments
                if (null == registryConnection)
                {
                    throw new ArgumentNullException("registryConnection");
                }
                #endregion

                Collection<AssemblyPackage> searchResults = null;

                try
                {
                    AssemblyPackage.ISearch searchQuery = AssemblyPackage.ISearch.Create();
                    searchQuery.Query = AssemblyPackage.ISearch.IsDeleted(BooleanField.Condition.Equals, false);
                    searchResults = new Collection<AssemblyPackage>(AssemblyPackage.Search(searchQuery, registryConnection));
                }
                catch (RecordNotFoundException)
                {

                }
                return searchResults;
            }

            public static Collection<AssemblyPackage> GetAssemblyPackage(Connection registryConnection, string packageName)
            {
                #region Validation of Arguments
                if (null == registryConnection)
                {
                    throw new ArgumentNullException("registryConnection");
                }
                if (string.IsNullOrEmpty(packageName))
                {
                    throw new ArgumentNullException("packageName");
                }
                #endregion

                Collection<AssemblyPackage> searchResults = null;

                try
                {
                    AssemblyPackage.ISearch searchQuery = AssemblyPackage.ISearch.Create();
                    searchQuery.Query = AssemblyPackage.ISearch.AND(AssemblyPackage.ISearch.Name(StringField.Condition.Equals, packageName),
                        AssemblyPackage.ISearch.IsDeleted(BooleanField.Condition.Equals, false));
                    searchResults = new Collection<AssemblyPackage>(AssemblyPackage.Search(searchQuery, registryConnection));
                }
                catch (RecordNotFoundException)
                {

                }
                return searchResults;
            }
        }
        #endregion

        /// <summary>
        /// Get the activities which match the name from the assembly package.
        /// </summary>
        /// <param name="activityFullName">The activity name.</param>
        /// <param name="package">The assembly package to search.</param>
        /// <param name="connection">The registry connection.</param>
        /// <returns>The collection of activities.</returns>
        public Collection<Activity> GetActivity(string activityFullName)
        {
            #region Validation of Arguments
            if (string.IsNullOrEmpty(activityFullName))
            {
                throw new ArgumentNullException("activityFullName");
            }
            #endregion
            Collection<Activity> activities = null;
            try
            {
                Activity.ISearch searchQuery = Activity.ISearch.Create();
                searchQuery.Query = Activity.ISearch.AND(
                    Activity.ISearch.ActivityClass(StringField.Condition.Equals, activityFullName.Trim()),
                Activity.ISearch.AssemblyPackage(RelationFieldBase.Condition.Equals, this));
                activities = new Collection<Activity>(Activity.Search(searchQuery, this.Conn));
            }
            catch (RecordNotFoundException)
            {
            }
            return activities;
        }         
    }
}
